package com.guat.service;

import com.guat.dao.ArticleDao;
import com.guat.dao.UserDao;
import com.guat.entity.Article;
import com.guat.entity.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ArticleService {

    //获取所有文章
    public List<Article> getAll(){
        ArticleDao articleDao = new ArticleDao();
        return articleDao.getArticleAll();
    }

    //添加文章
    public Map<String, Object> addArticle(String title, String content) {
        ArticleDao articleDao = new ArticleDao();
        Map<String, Object> map = new HashMap<>();
        boolean flag = articleDao.addArticle(title, content);
        if (flag){
            map.put("code",200);
            map.put("msg","添加文章成功！");
        }else {
            map.put("code",400);
            map.put("msg","添加文章失败！");
        }
        return map;
    }

    //删除文章
    public Map<String, Object> deleteArticle(String id){
        ArticleDao articleDao = new ArticleDao();
        Map<String, Object> map = new HashMap<>();
        boolean flag = articleDao.deleteArticle(id);
        if (flag) {
            map.put("code",200);
            map.put("msg","删除成功！");
        } else {
            map.put("code",400);
            map.put("msg","删除失败,文章不存在！");
        }
        return map;
    }

    //先根据id获取想要修改文章的信息,响应给前端放入输入框
    public Map<String, Object> doUpdateArticle(String id){
        ArticleDao articleDao = new ArticleDao();
        Map<String, Object> map = new HashMap<>();
        Article article = articleDao.doUpdateArticle(id);
        map.put("code",200);
        map.put("msg","查询成功！");
        map.put("data",article);
        return map;
    }

    //根据id修改文章的信息
    public Map<String, Object> updateArticle(String id,String title,String content){
        ArticleDao articleDao = new ArticleDao();
        Map<String, Object> map = new HashMap<>();
        articleDao.updateArticle(id,title,content);
        map.put("code",200);
        map.put("msg","修改成功！");
        return map;
    }

    //根据搜索值获取文章
    public List<Article> searchArticle(String value){
        ArticleDao articleDao = new ArticleDao();
        return articleDao.searchArticle(value);
    }
}
